var index=1;
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/apps/notice/index',
                    add_url: '',
                    edit_url: 'facrm/apps/notice/edit',
                    del_url: '',
                    multi_url: '',
                    table: '',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {field: 'describe', title: __('名称'), operate: "LIKE"},
                        {
                            field: 'status',
                            title: __('Status'),
                            formatter: Table.api.formatter.status,
                            searchList: {0: __('禁用'), 1: __('正常')}
                        },

                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'edit',
                                    text: '设置',
                                    title: '设置',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: function (row) {
                                        return 'facrm/apps/notice/edit/key/'+row.key;
                                    }
                                },
                            ]
                        }

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.func.smspublic();
            Controller.api.bindevent();

        },
        edit: function () {
            Controller.api.func.smspublic();
            Controller.api.bindevent();

        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"),function () {
                    parent.window.location.reload();
                });
                $("input[type=radio][name='row[config]']").change(function(){
                    var type=$(this).val();
                    if(type==1){
                        $('.table-responsive').show();
                    }else{
                        $('.table-responsive').hide();
                    }
                });
            },
            func: {

                getUrlParam(name) {
                    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
                    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
                    if (r != null) return unescape(r[2]); return null; //返回参数值
                },
                smspublic(){
                    $(document).on("change", "#c-param_type", function(){
                        var param_type= $(this).val();
                        Fast.api.ajax({
                                url:'facrm/apps/sms/getfield/types/'+param_type,
                            }
                            , function (data) {
                                $('.fieldlist').data('tpl',data);
                                //移除之前的
                                $(".fieldlist dd .btn-remove").each(function(){
                                    $(this).trigger("click");
                                });
                                return false;
                            }, function () {

                                return ;
                            });
                    });


                    $(document).on("fa.event.appendfieldlist", "form[role=form] .btn-append", function (e, obj) {
                        Form.events.selectpicker(obj);
                    });
                }

            }
        }
    };
    return Controller;
});


